import { Component, OnInit } from '@angular/core';
import { ActivationEnd, Router } from '@angular/router';
import { Subscription } from 'rxjs';
import { filter } from 'rxjs/operators';

@Component({
  selector: 'app-business-shipment-inbound',
  templateUrl: './shipment-inbound.component.html'
})
export class BusinessShipmentInboundComponent implements OnInit {
  private router$!: Subscription;
  tabs = [
    {
      key: 'shipment',
      tab: '货件'
    },
    {
      key: 'inbound-plan',
      tab: '入库计划'
    }
  ];
  pos = 0;

  constructor(private router: Router) {}

  private setActive(): void {
    const key = this.router.url.substring(this.router.url.lastIndexOf('/') + 1);
    const idx = this.tabs.findIndex(w => w.key === key);
    if (idx !== -1) {
      this.pos = idx;
    }
  }

  ngOnInit(): void {
    this.router$ = this.router.events.pipe(filter(e => e instanceof ActivationEnd)).subscribe(() => this.setActive());
    this.setActive();
  }

  to(item: { key: string }): void {
    this.router.navigateByUrl(`/business/shipment-inbound/${item.key}`).then(() => {});
  }

  ngOnDestroy(): void {
    this.router$.unsubscribe();
  }
}
